<?php


// gets the game from the database
function load_game($game_id)
{
	global $mysql;

	// clear out the previous data
	$_SESSION['gamedata'] = null;
	unset($_SESSION['gamedata']);

	$query = "
		SELECT *
		FROM ".T_GAME."
		WHERE g_id = '{$game_id}'
	";
	$gamedata = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['game_id'] = $game_id;
	$_SESSION['gamedata']['status']      = $gamedata['g_status'];
	$_SESSION['gamedata']['board_size']  = $gamedata['g_board_size'];
	$_SESSION['gamedata']['handicap']    = $gamedata['g_handicap'];
	$_SESSION['gamedata']['move_string'] = $gamedata['g_moves'];
	$_SESSION['gamedata']['moves']       = explode(',', $gamedata['g_moves']);
	$_SESSION['gamedata']['num_moves']   = count($_SESSION['gamedata']['moves']);
	$_SESSION['gamedata']['private_chat_box'] = $gamedata['g_private_chat_box'];

	$query = "
		SELECT *
		FROM ".T_PLAYER."
		WHERE p_id = '{$gamedata['g_black_player_id']}'
	";
	$gamedata['p_black'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['gamedata']['black']['id']       = $gamedata['p_black']['p_id'];
	$_SESSION['gamedata']['black']['username'] = $gamedata['p_black']['p_username'];
	$_SESSION['gamedata']['black']['email']    = $gamedata['p_black']['p_email'];

	$query = "
		SELECT *
		FROM ".T_PLAYER."
		WHERE p_id = '{$gamedata['g_white_player_id']}'
	";
	$gamedata['p_white'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['gamedata']['white']['id']       = $gamedata['p_white']['p_id'];
	$_SESSION['gamedata']['white']['username'] = $gamedata['p_white']['p_username'];
	$_SESSION['gamedata']['white']['email']    = $gamedata['p_white']['p_email'];

	if (( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator'])) && (('Black' == $gamedata['g_status']) || ('White' == $gamedata['g_status'])))
	{
		$_SESSION['gamedata'][strtolower($gamedata['g_status'])]['move'] = true;
	}

	if ($_SESSION['player_id'] == $gamedata['g_black_player_id'])
	{
		$_SESSION['gamedata']['player'] = $_SESSION['gamedata']['black'];
		$_SESSION['gamedata']['player']['color'] = 'black';

		$_SESSION['gamedata']['opponent'] = $_SESSION['gamedata']['white'];
		$_SESSION['gamedata']['opponent']['color'] = 'white';
	}
	else
	{
		$_SESSION['gamedata']['player'] = $_SESSION['gamedata']['white'];
		$_SESSION['gamedata']['player']['color'] = 'white';

		$_SESSION['gamedata']['opponent'] = $_SESSION['gamedata']['black'];
		$_SESSION['gamedata']['opponent']['color'] = 'black';
	}

	// make sure ['player']['move'] has data
	if ( ! isset($_SESSION['gamedata']['player']['move']))
	{
		$_SESSION['gamedata']['player']['move'] = false;
	}

	return true;
}


function save_game($game_id, $move, $repeats, $removed = '')
{
	global $mysql;

	// get the current values
	$status      = $_SESSION['gamedata']['status'];
	$num_moves   = $_SESSION['gamedata']['num_moves'];
	$move_string = $_SESSION['gamedata']['move_string'];

	if (substr($move, 1, 2) == 'zz') // resigned
	{
		if (substr($move, 0, 1) == 'b')
		{
			$status = 'Black Resigned';
		}
		else
		{
			$status = 'White Resigned';
		}

		$move_string .= ('' != $move_string) ? ',' . $move : $move;
	}
	else if (substr($move, 1, 2) == 'xx') // passed
	{
		$status = ('Black' == $status) ? 'White' : 'Black';

		if (intval($repeats) == 2)
		{
			$status = 'Game Over';
		}

		$move_string .= ('' != $move_string) ? ',' . $move : $move;
		$num_moves++;
	}
	else // normal move
	{
		$status = ('Black' == $status) ? 'White' : 'Black';
		$move_string .= ('' != $move_string) ? ',' . $move . $removed : $move . $removed;
		$num_moves++;
	}

	$query = "
		UPDATE ".T_GAME."
		SET g_moves = '{$move_string}'
			, g_num_moves = '{$num_moves}'
			, g_status = '{$status}'
			, g_last_move = NOW( )
		WHERE g_id = '{$_SESSION['game_id']}'
	";
	$mysql->query($query, __LINE__, __FILE__);

	return true;
}


// plays the given moves to the board, used to recreate board from database
function play_moves($moves, $board, $boardsize, $handicap)
{
	$bprisoners = 0;
	$wprisoners = 0;
	$passes     = 0;
	$resign     = 0;
	$history    = array( );
	$countm     = count($moves);

	$oldboard = false;
	$mstonex = -1;
	$mstoney = -1;

	$until = isset($_GET['until']) ? $_GET['until'] : $countm + 1;

	if ($handicap > 0)
	{
		$board = set_handicap_stones($board, $boardsize, $handicap);
	}

	for ($i = 0; $i < $countm; ++$i)
	{
		for ($j = 0; $j < strlen($moves[$i]); $j = $j + 3)
		{
			list($color, $y, $x) = stringtocord(substr($moves[$i], $j, 3));

			if ('e' == $color)
			{
				if ($until > $i)
				{
					$board[$y][$x] = insert_empty($y, $x, $boardsize);
				}

				++$wprisoners;
			}
			else if ('r' == $color)
			{
				if ($until > $i)
				{
					$board[$y][$x] = insert_empty($y, $x, $boardsize);
				}

				++$bprisoners;
			}
			else if ((23 == $y) && (23 == $x))
			{
				if (($i == ($countm - 1)) && ($until == ($countm + 1)) || ($i == ($until - 1)))
				{
					$mstonex = -1;
					$mstoney = -1;
				}

				if ('b' == $color)
				{
					$cprint = 'Black';
				}

				if ('w' == $color)
				{
					$cprint = 'White';
				}

				++$passes;
				$history[$i] = 'P';
			}
			else if ((25 == $y) && (25 == $x))
			{
				if (($i == ($countm - 1)) && ($until == ($countm + 1)) || ($i == ($until - 1)))
				{
					$mstonex = -1;
					$mstoney = -1;
				}

				if ('b' == $color)
				{
					$cprint = 'Black';
					$resign = 1;
				}

				if ('w' == $color)
				{
					$cprint = 'White';
					$resign = 2;
				}

				$passes      = 0;
				$history[$i] = 'R';
			}
			else
			{
				if ((($i == ($countm - 1)) && ($until == ($countm + 1))) || ($i < $until))
				{
					$mstonex = $x;
					$mstoney = $y;
				}

				if ($until > $i)
				{
					$board[$y][$x] = $color;
				}

				if ('b' == $color)
				{
					$cprint = 'Black';
				}

				if ('w' == $color)
				{
					$cprint = 'White';
				}

				$history[$i] = strtolower(xtoalt($x)) . ($boardsize - $y);
				$passes = 0;
			}
		}

		if ($i == ($countm - 2))
		{
			$oldboard = $board;
		}
	}

	return array(
		$board ,
		$oldboard ,
		$passes ,
		$resign ,
		$bprisoners ,
		$wprisoners ,
		$mstonex ,
		$mstoney ,
		$history ,
		$countm ,
	);
}


function set_handicap_stones($board, $size_of_the_board, $number)
{
	if (2 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[3][$size_of_the_board - 4] = 'b';
		}

		if ($size_of_the_board < 13)
		{
			$board[2][$size_of_the_board - 3] = 'b';
		}

		if ($size_of_the_board >= 13)
		{
			$board[$size_of_the_board - 4][3] = 'b';
		}

		if ($size_of_the_board < 13)
		{
			$board[$size_of_the_board - 3][2] = 'b';
		}
	}

	if (3 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[$size_of_the_board - 4][$size_of_the_board - 4] = 'b';
		}

		if ($size_of_the_board < 13)
		{
			$board[$size_of_the_board - 3][$size_of_the_board - 3] = 'b';
		}
	}

	if (4 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[3][3] = 'b';
		}

		if ($size_of_the_board < 13)
		{
			$board[2][2] = 'b';
		}
	}

	if (5 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[ceil($size_of_the_board / 2) - 1][$size_of_the_board - 4] = 'b';
		}

		if ($size_of_the_board < 13 && $size_of_the_board > 7)
		{
			$board[ceil($size_of_the_board / 2) - 1][$size_of_the_board - 3] = 'b';
		}
	}

	if (6 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[ceil($size_of_the_board / 2) - 1][3] = 'b';
		}

		if ($size_of_the_board < 13 && $size_of_the_board > 7)
		{
			$board[ceil($size_of_the_board / 2) - 1][2] = 'b';
		}
	}

	if (7 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[3][ceil($size_of_the_board / 2) - 1] = 'b';
		}

		if ($size_of_the_board < 13 && $size_of_the_board > 7)
		{
			$board[2][ceil($size_of_the_board / 2) - 1] = 'b';
		}
	}

	if (8 <= $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[$size_of_the_board - 4][ceil($size_of_the_board / 2) - 1] = 'b';
		}

		if ($size_of_the_board < 13 && $size_of_the_board > 7)
		{
			$board[$size_of_the_board - 3][ceil($size_of_the_board / 2) - 1] = 'b';
		}
	}

	if (9 == $number)
	{
		if ($size_of_the_board >= 13)
		{
			$board[ceil($size_of_the_board / 2) - 1][ceil($size_of_the_board / 2) - 1] = 'b';
		}

		if ($size_of_the_board < 13 && $size_of_the_board > 7)
		{
			$board[ceil($size_of_the_board / 2) - 1][ceil($size_of_the_board / 2) - 1] = 'b';
		}
	}

	return $board;
}


// translate numeric coordinates to alphanumeric coordinates
function xtoalt($xcord, $full = false)
{
	if ($full || ($xcord < 8))
	{
		return chr(65 + $xcord);
	}
	else
	{
		return chr(66 + $xcord);
	}
}


// translates alphanumeric coordinates to numeric coordinates
function stringtocord($costring)
{
	$color = substr($costring, 0, 1);
	$y     = ord(substr($costring, 1, 1)) - 97;
	$x     = ord(substr($costring, 2, 1)) - 97;
	return array(
		$color ,
		$y ,
		$x ,
	);
}


// insert an empty field on the given position into the board array
function insert_empty($y, $x, $size_of_the_board)
{
	if ($y == 0)
	{
		if ($x == 0)
		{
			$stone = 'lt';
		}
		else if ($x == $size_of_the_board - 1)
		{
			$stone = 'rt';
		}
		else
		{
			$stone = 'mt';
		}
	}
	else if ($y == $size_of_the_board - 1)
	{
		if ($x == 0)
		{
			$stone = 'lb';
		}
		else if ($x == $size_of_the_board - 1)
		{
			$stone = 'rb';
		}
		else
		{
			$stone = 'mb';
		}
	}
	else
	{
		if ($x == 0)
		{
			$stone = 'lm';
		}
		else if ($x == $size_of_the_board - 1)
		{
			$stone = 'rm';
		}
		else
		{
			if ($size_of_the_board >= 13 && ($y == 3 && ($x == 3 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 4)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board >= 13 && ($y == ceil($size_of_the_board / 2) - 1 && ($x == 3 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 4)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board >= 13 && ($y == $size_of_the_board - 4 && ($x == 3 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 4)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board < 13 && $size_of_the_board > 7 && ($y == 2 && ($x == 2 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 3)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board < 13 && $size_of_the_board > 7 && ($y == ceil($size_of_the_board / 2) - 1 && ($x == 2 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 3)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board < 13 && $size_of_the_board > 7 && ($y == $size_of_the_board - 3 && ($x == 2 || $x == ceil($size_of_the_board / 2) - 1 || $x == $size_of_the_board - 3)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board == 7 && ($y == 2 && ($x == 2 || $x == $size_of_the_board - 3)))
			{
				$stone = 'ko';
			}
			else if ($size_of_the_board == 7 && ($y == $size_of_the_board - 3 && ($x == 2 || $x == $size_of_the_board - 3)))
			{
				$stone = 'ko';
			}
			else
			{
				$stone = 'mm';
			}
		}
	}

	return $stone;
}


// do a move a user requested, take prisoners
function do_move($y, $x, $color)
{
	global $board;
	$board[$y][$x] = $color;

	if ($color == 'b')
	{
		$dummy = group_runs_out_of_liberties($y, $x, true, 'w');
	}
	else if ($color == 'w')
	{
		$dummy = group_runs_out_of_liberties($y, $x, true, 'b');
	}
	else
	{
		echo 'Error: Given color attribute is not known !!<br />';
		echo 'Attribute was: ' . $color . '<br />';
		echo 'Execution halted !!<br />';
		exit;
	}
}


// return true if the move is ko and therfore not allowed, else return false
function is_ko($y, $x, $color)
{
	global $game;

	global $board;
	global $oldboard;

	$backup = $board;
	do_move($y, $x, $color);

	for ($i = 0; $i < $game['board_size']; ++$i)
	{
		for ($j = 0; $j < $game['board_size']; ++$j)
		{
			if ($board[$i][$j] == $oldboard[$i][$j])
			{
				$ko = true;
			}
			else
			{
				$ko = false;
				break;
			}
		}

		if ($ko == false)
		{
			break;
		}
	}

	$board = $backup;
	return $ko;
}


// return true if the point on the board is a free space
function is_freedom($y, $x, $board)
{
	return (('b' != $board[$y][$x]) && ('w' != $board[$y][$x]));
}


// return true if the move is not a suicide, this function does not check for ko se the is_ko function for this
function is_legal_move($y, $x, $color)
{
	global $board;
	global $visited;
	global $filename;

	if ($board[$y][$x] == 'lt' || $board[$y][$x] == 'mt' || $board[$y][$x] == 'rt' || $board[$y][$x] == 'lm' || $board[$y][$x] == 'mm' || $board[$y][$x] == 'rm' || $board[$y][$x] == 'lb' || $board[$y][$x] == 'mb' || $board[$y][$x] == 'rb' || $board[$y][$x] == 'ko')
	{
		$backup = $board[$y][$x];

		if ($color == 'b')
		{
			$board[$y][$x] = 'b';

			if (freedom_of_group($y, $x, $color) < 1)
			{
				$board[$y][$x] = 'b';

				if ( ! group_runs_out_of_liberties($y, $x, false, 'w'))
				{
					$board[$y][$x] = $backup;
					return false;
				}
				else
				{
					$board[$y][$x] = $backup;
					return true;
				}
			}

			$board[$y][$x] = $backup;
			return true;
		}
		else if ($color == 'w')
		{
			$board[$y][$x] = 'w';

			if (freedom_of_group($y, $x, $color) < 1)
			{
				if ( ! group_runs_out_of_liberties($y, $x, false, 'b'))
				{
					$board[$y][$x] = $backup;
					return false;
				}
				else
				{
					$board[$y][$x] = $backup;
					return true;
				}
			}

			$board[$y][$x] = $backup;
			return true;
		}
		else
		{
			echo 'Error: Given color attribute is not known !!<br />';
			echo 'Attribute was: ' . $color . '<br />';
			echo 'Execution halted !!<br />';
			die;
		}
	}
}


// delete the stones last checked by another function
function delete_current_visited($color)
{
	global $game;
	global $board;
	global $visited;
	global $removed;

	$boardsize     = $game['board_size'];
	$pos_to_delete = strtok($visited, ',');

	while (strlen($pos_to_delete) >= 1)
	{
		if (strlen($pos_to_delete) >= 1)
		{
			if (intval($pos_to_delete) >= 100)
			{
				$i = floor(intval($pos_to_delete) / 100);
			}
			else
			{
				$i = 0;
			}

			$i2 = intval($pos_to_delete) - ($i * 100);

			if ($color == 'b')
			{
				$rcolor = 'e';
			}
			else if ($color == 'w')
			{
				$rcolor = 'r';
			}
			else
			{
				echo 'Error';
				die;
			}

			if ( ! strstr($removed, $rcolor . chr($i + 97) . chr($i2 + 97)))
			{
				$removed = $removed . $rcolor . chr($i + 97) . chr($i2 + 97);
			}

			$board[$i][$i2] = insert_empty($i, $i2, $boardsize);
		}

		$pos_to_delete = strtok(',');
	}
}


// return all board positions which are connected by lines to the current one
function is_connected_to($y, $x)
{
	global $game;
	$boardsize = $game['board_size'];

	$connected = array( );

	if ($y == 0)
	{
		if ($x == 0)
		{
			$connected[0] = $y + 1;
			$connected[1] = $x;
			$connected[2] = $y;
			$connected[3] = $x + 1;
		}
		else if ($x == $boardsize - 1)
		{
			$connected[0] = $y + 1;
			$connected[1] = $x;
			$connected[2] = $y;
			$connected[3] = $x - 1;
		}
		else
		{
			$connected[0] = $y;
			$connected[1] = $x - 1;
			$connected[2] = $y;
			$connected[3] = $x + 1;
			$connected[4] = $y + 1;
			$connected[5] = $x;
		}
	}
	else if ($y == $boardsize - 1)
	{
		if ($x == 0)
		{
			$connected[0] = $y - 1;
			$connected[1] = $x;
			$connected[2] = $y;
			$connected[3] = $x + 1;
		}
		else if ($x == $boardsize - 1)
		{
			$connected[0] = $y - 1;
			$connected[1] = $x;
			$connected[2] = $y;
			$connected[3] = $x - 1;
		}
		else
		{
			$connected[0] = $y;
			$connected[1] = $x - 1;
			$connected[2] = $y;
			$connected[3] = $x + 1;
			$connected[4] = $y - 1;
			$connected[5] = $x;
		}
	}
	else
	{
		if ($x == 0)
		{
			$connected[0] = $y - 1;
			$connected[1] = $x;
			$connected[2] = $y + 1;
			$connected[3] = $x;
			$connected[4] = $y;
			$connected[5] = $x + 1;
		}
		else if ($x == $boardsize - 1)
		{
			$connected[0] = $y - 1;
			$connected[1] = $x;
			$connected[2] = $y + 1;
			$connected[3] = $x;
			$connected[4] = $y;
			$connected[5] = $x - 1;
		}
		else
		{
			$connected[0] = $y + 1;
			$connected[1] = $x;
			$connected[2] = $y - 1;
			$connected[3] = $x;
			$connected[4] = $y;
			$connected[5] = $x + 1;
			$connected[6] = $y;
			$connected[7] = $x - 1;
		}
	}

	return $connected;
}


// return true if group is dead
function group_runs_out_of_liberties($y, $x, $delete_dead_stones, $color)
{
	global $board;

	$they_are_dead_jim = false;
	$counter           = 0;
	$to_check          = is_connected_to($y, $x);

	foreach ($to_check as $number => $coord)
	{
		if ($counter % 2 == 0)
		{
			if ($board[$to_check[$counter]][$to_check[$counter + 1]] == $color)
			{
				if (freedom_of_group($to_check[$counter], $to_check[$counter + 1], $color) < 1)
				{
					if ($delete_dead_stones)
					{
						delete_current_visited($color);
					}

					$they_are_dead_jim = true;
				}
			}
		}

		++$counter;
	}

	return $they_are_dead_jim;
}


// returns the owner and the number of freedoms grouped together
function group_of_freedoms($y, $x)
{
	global $visited;
	$visited = ',';

	global $stones_visited;
	$stones_visited = ',';

	global $board;
	global $game;
	$boardsize = $game['board_size'];

	$to_visit_string = ',' . strval(($y * 100) + $x) . ',';
	$owner           = '';
	$freedom         = 0;

	while (strlen($to_visit_string) > 2)
	{
		$cord_to_visit = strtok(substr($to_visit_string, 1), ',');

		if (intval($cord_to_visit) >= 100)
		{
			$y = floor(intval($cord_to_visit) / 100);
		}
		else
		{
			$y = 0;
		}

		$x = intval($cord_to_visit) - ($y * 100);
		$to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
		$counter = 0;
		$to_check = is_connected_to($y, $x);

		foreach ($to_check as $number => $coord)
		{
			if ($counter % 2 == 0)
			{
				if ( ! strstr($stones_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
				{
					if ( ! is_freedom($to_check[$counter], $to_check[$counter + 1], $board))
					{
						if ('' != $owner)
						{
							if ($owner != $board[$to_check[$counter]][$to_check[$counter + 1]])
							{
								$owner = 'f';
							}
						}
						else
						{
							$owner = $board[$to_check[$counter]][$to_check[$counter + 1]];
						}

						$stones_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
					}
				}

				if ( ! strstr($visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
				{
					if (is_freedom($to_check[$counter], $to_check[$counter + 1], $board))
					{
						++$freedom;
						$to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
					}
				}
			}

			++$counter;
		}

		$visited .= strval(($y * 100) + $x) . ',';
	}

	return array(
		$owner ,
		$freedom ,
	);
}


// how many liberties does a group of stones have
function freedom_of_group($y, $x, $color)
{
	global $visited;
	$visited = ',';

	global $freedoms_visited;
	$freedoms_visited = ',';

	global $board;
	global $game;

	$boardsize = $game['board_size'];

	$to_visit_string = ',' . strval(($y * 100) + $x) . ',';
	$freedom         = 0;

	while (strlen($to_visit_string) > 2)
	{
		$cord_to_visit = strtok(substr($to_visit_string, 1), ',');

		if (intval($cord_to_visit) >= 100)
		{
			$y = floor(intval($cord_to_visit) / 100);
		}
		else
		{
			$y = 0;
		}

		$x = intval($cord_to_visit) - ($y * 100);
		$to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
		$counter = 0;
		$to_check = is_connected_to($y, $x);

		foreach ($to_check as $number => $coord)
		{
			if ($counter % 2 == 0)
			{
				if ( ! strstr($freedoms_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
				{
					if (is_freedom($to_check[$counter], $to_check[$counter + 1], $board))
					{
						++$freedom;
						$freedoms_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
					}
				}

				if ( ! strstr($visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
				{
					if ($board[$to_check[$counter]][$to_check[$counter + 1]] == $color)
					{
						$to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
					}
				}
			}

			++$counter;
		}

		$visited .= strval(($y * 100) + $x) . ',';
	}

	return $freedom;
}


// creates an empty board to start with
function create_empty_board($size_of_the_board)
{
	for ($y = 0; $y < $size_of_the_board; ++$y)
	{
		for ($x = 0; $x < $size_of_the_board; ++$x)
		{
			$board[$y][$x] = insert_empty($y, $x, $size_of_the_board);
		}
	}

	return $board;
}

?>